package ictbk.ttmk51.grapro.app.safesms;

import java.util.ArrayList;

import ictbk.ttmk51.grapro.app.LoginActivity;
import ictbk.ttmk51.grapro.app.R;
import ictbk.ttmk51.grapro.app.common.MyActivity;
import ictbk.ttmk51.grapro.app.service.GetWebservice;
import ictbk.ttmk51.grapro.app.utils.DateFormat;
import ictbk.ttmk51.grapro.app.utils.SMSProvider;
import android.app.AlertDialog;
import android.app.ProgressDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout;

public class SafeSMSActivity extends MyActivity {
	
	public static String xmlString = "";
	Handler								myHandler				= new Handler();
	
	SMSProvider smsProvider = new SMSProvider();
	
	
	public void onCreate(Bundle savedInstanceState){
		super.onCreate(savedInstanceState);
		setContentView(R.layout.safesms_screen);
		
		Button btnBackupSMS = (Button)findViewById(R.id.btnBackupSMS);
		Button btnRestoreSMS = (Button)findViewById(R.id.btnRestoreSMS);
		
		btnBackupSMS.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				showBackupSMSDialog();
			}
		});
		
		btnRestoreSMS.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				Intent intent = new Intent(SafeSMSActivity.this, RestoreSMSActivity.class);
				startActivity(intent);
			}
		});
		
	}
	
	/*
	public void RestoreSMS(){
		//do something
		Toast.makeText(SafeSMSActivity.this, "Restore SMS Successfuly!", Toast.LENGTH_SHORT).show();
		SMSProvider smsProvider = new SMSProvider();
		smsProvider.writeSMS(SafeSMSActivity.this, MyXStream.getSMSListfromXML(xmlString));
	}
	*/
	
	//Backup SMS Dialog
	public void showBackupSMSDialog() {
		final LinearLayout ll = new LinearLayout(this);
		final EditText inputBoxNotes = new EditText(this, null);

		ll.setOrientation(LinearLayout.VERTICAL);
		ll.setPadding(5, 5, 5, 5);
		ll.addView(inputBoxNotes, new LinearLayout.LayoutParams(LinearLayout.LayoutParams.FILL_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT));
		inputBoxNotes.setHint("Notes");
		new AlertDialog.Builder(this).setView(ll).setIcon(R.drawable.backup_icon).setTitle("Backup Contact").setPositiveButton("OK", new DialogInterface.OnClickListener() {
			public void onClick(DialogInterface d, int which) {
				final ProgressDialog pd = showProgressDialog(null, "Please waiting...", false);
				new Thread() {
					public void run() {
						ArrayList<SMS> SMSList = smsProvider.getAllSMSList(SafeSMSActivity.this);
						String notes = inputBoxNotes.getText().toString();
						try {
							if (GetWebservice.backupSMS(SMSList, LoginActivity.getUserID(), getDate(DateFormat.DDMMYYYY, "-"), getTime(), notes)) {
								myHandler.post(new Runnable() {
									public void run() {
										showToast("Backup SMS completed");
									}
								});
							} else {
								myHandler.post(new Runnable() {
									public void run() {
										showToast("Backup SMS error!");
									}
								});
							}
						} catch (final Exception e) {
							myHandler.post(new Runnable() {
								public void run() {
									showErrorDialog("Please check again connection." + e.getMessage());
								}
							});
						}
						pd.dismiss();
					}
				}.start();
			}
		}).setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
			public void onClick(DialogInterface d, int which) {
				d.dismiss();
			}
		}).create().show();
	}
}
